package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.dao.IOutInventoryDao;
import com.easy.service.IOutInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/outinventory")
public class OutInventoryController {
    @Autowired
    IOutInventoryService is;
    @Autowired
    IOutInventoryDao dao;

    @GetMapping("/{out_id}")
    public CommonResult getInInventoryByInId(@PathVariable String out_id) {
        List<Order> orders = dao.getOutInventoryByInId(out_id);
        return CommonResult.success(orders);
    }

    @GetMapping("/all")
    public CommonResult getIOutInventory() {
        List<Order> list = is.getOutInventory();
        return CommonResult.success(list);
    }

    @PostMapping("/add")
    public CommonResult addOutInventory(@RequestBody Order order){
        int count = is.addOutInventory(order);
        if(count>0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }    }

    @PutMapping("/edit/{id}")
    public CommonResult editOutInventory(@PathVariable int id, @RequestBody Order order){
        order.setId(id);  // 将URL中的id设置到User对象中
        int count = is.editOutInventory(order);
        if(count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    @DeleteMapping("/del/{id}")
    public CommonResult delOutInventory(@PathVariable int id) {
        int count = is.delOutInventory(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    //  查询商品名或id
    @GetMapping("/search")
    public CommonResult searchOutInventory(@RequestParam String keyword){
        List<Order> inventories = is.searchOutInventory(keyword);
        return CommonResult.success(inventories);
    }

    //   分页
    @GetMapping("/paged")
    public CommonResult getPagedOutInventory(@RequestParam int page, @RequestParam int size) {
        List<Order> list = is.getPagedOutInventory(page, size);
        int total = is.getTotalOutInventoryCount();
        return CommonResult.success(list, total);
    }
}
